package jgl.vajra.operator.part.mine.activity;

import android.text.TextUtils;

import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jgl.vajra.operator.R;
import jgl.vajra.operator.bean.CarNameBean;
import jgl.vajra.operator.databinding.ActivityCarNameBinding;
import jgl.vajra.operator.manager.AccountManager;
import jgl.vajra.operator.part.mine.contract.CarNameContract;
import jgl.vajra.operator.part.mine.viewModel.CarNameViewModel;
import jgl.vajra.operator.utils.RandomUtils;
import jgl.vajra.operator.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.operator.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.operator.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.operator.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.operator.widget.retrofithelper.utils.AEACCoder;
import jgl.vajra.operator.widget.utils.ToastUtils;
import jgl.vajra.operator.widget.utils.UIUtils;

@CreateViewModel(CarNameViewModel.class)
public class CarNameActivity extends BaseMVVMActivity<CarNameViewModel, ActivityCarNameBinding> implements CarNameContract.View, BaseBindingItemPresenter<CarNameBean> {

    private String logo;
    private String authorization;
    private SingleTypeBindingAdapter adapter;
    private List<CarNameBean> list;

    @Override
    public int getLayoutId() {
        return R.layout.activity_car_name;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);
        authorization = AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization;
        String titleName = getIntent().getStringExtra("titleName");
        logo = getIntent().getStringExtra("logo");
        mBinding.etName.setText(titleName);

        // 不可嵌套滑动
        mBinding.recyclerView.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerView.setFocusableInTouchMode(false);

        FlexboxLayoutManager flManager = new FlexboxLayoutManager(this);
        flManager.setFlexWrap(FlexWrap.WRAP);
        //设置主轴的方向
        flManager.setFlexDirection(FlexDirection.ROW);
        //设置布局管理器
        mBinding.recyclerView.setLayoutManager(flManager);

        adapter = new SingleTypeBindingAdapter(this, null, R.layout.item_city_choice);
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setItemPresenter(this);

        requestNetData();
    }

    private void requestNetData() {
        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AEACCoder.encrypt(RandomUtils.getRandom()));//随机数
        mViewModel.getCarDefaultName(map);
    }

    //保存
    @Override
    public void onDetermine() {
        ToastUtils.showShort("保存");
        String titleName = mBinding.etName.getText().toString().trim();

        if (TextUtils.isEmpty(titleName)) {
            ToastUtils.showShort("输入豪车名称");
            return;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", authorization);
        map.put("logo", logo);
        map.put("titleName", URLEncoder.encode(titleName));
        mViewModel.carModifyTitle(map);
    }

    @Override
    public void returnCarModifyTitle(String data) {
        ToastUtils.showShort("修改成功");
        finish();
    }

    @Override
    public void returnCarDefaultName(List<String> data) {

        list = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            CarNameBean bean = new CarNameBean();
            bean.name = data.get(i);
            bean.isClick = false;
            list.add(bean);
        }
        adapter.refresh(list);
    }

    @Override
    public void onItemClick(int position, CarNameBean itemData) {
        if (itemData.isClick)
            return;
        for (int i = 0; i < list.size(); i++) {
            list.get(i).isClick = false;
        }
        list.get(position).isClick = true;
        mBinding.etName.setText(itemData.name);
        adapter.refresh();
    }
}
